package com.tianye.lc.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.SaveCallback;
import com.tianye.lc.R;
import com.tianye.lc.common.Final;

public class AddOrShowOrEditActivity extends BaseActivity {
    private EditText nameEt, typeEt,bloomEt,multipleSeasonsEt,diseaseResistantEt;
    private ImageView sureIv;
    private TextView mTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        initTitle();
        initView();
        initData();

    }

    private void initTitle() {
        mTitle = (TextView) findViewById(R.id.title_tv);
        mTitle.setText("添加品种");
    }

    private void initView() {
        nameEt = (EditText) findViewById(R.id.edit_name_et);
        typeEt = (EditText) findViewById(R.id.edit_type_et);
        bloomEt = (EditText) findViewById(R.id.edit_bloom_et);
        multipleSeasonsEt = (EditText) findViewById(R.id.edit_multiple_easons_et);
        diseaseResistantEt = (EditText) findViewById(R.id.edit_disease_resistant_et);

        sureIv = (ImageView) findViewById(R.id.right_iv);
        sureIv.setVisibility(View.VISIBLE);
        sureIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent.getBooleanExtra(Final.IS_SHOW, false)) {
            mTitle.setText("查看品种");
            String id = intent.getStringExtra(Final.ROSE_ID);
            if (TextUtils.isEmpty(id)) {
                return;
            }
            show(id);
        }
    }

    private void save() {
        String name = nameEt.getText().toString();
        String type = typeEt.getText().toString();
        String bloom = bloomEt.getText().toString();
        String multipleSeasons = multipleSeasonsEt.getText().toString();
        String diseaseResistant = diseaseResistantEt.getText().toString();

        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(type)) {
            Toast.makeText(this, "请将信息填写完整", Toast.LENGTH_SHORT).show();
            return;
        }
        AVObject rose = new AVObject(Final.ROSE);
        rose.put("name", name);
        rose.put("type", type);
        rose.put("bloom", bloom);
        rose.put("multipleSeasons", multipleSeasons);
        rose.put("diseaseResistant", diseaseResistant);
        rose.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    // 存储成功
                    Toast.makeText(AddOrShowOrEditActivity.this, "保存成功！", Toast.LENGTH_SHORT).show();
                    nameEt.setText("");
                    typeEt.setText("");
                } else {
                    // 失败的话，请检查网络环境以及 SDK 配置是否正确
                    Toast.makeText(AddOrShowOrEditActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void show(String roseId) {
        sureIv.setVisibility(View.GONE);
        nameEt.setEnabled(false);
        typeEt.setEnabled(false);
        bloomEt.setEnabled(false);
        multipleSeasonsEt.setEnabled(false);
        diseaseResistantEt.setEnabled(false);
        // 第一参数是 className,第二个参数是 objectId
        AVObject todo = AVObject.createWithoutData(Final.ROSE, roseId);
        todo.fetchInBackground(new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                String name = avObject.getString("name");
                String type = avObject.getString("type");
                String bloom = avObject.getString("bloom");
                String multipleEasons = avObject.getString("multipleSeasons");
                String diseaseResistant = avObject.getString("diseaseResistant");

                nameEt.setText(name);
                typeEt.setText(type);
                bloomEt.setText(bloom);
                multipleSeasonsEt.setText(multipleEasons);
                diseaseResistantEt.setText(diseaseResistant);
            }
        });
    }

}
